package com.zd.commodity.brandMng.dao.impl;

import java.sql.Timestamp;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.zd.commodity.brandMng.dao.IBrandShowDao;
import com.zd.commodity.brandMng.entity.BrandShow;
import com.zd.commodity.dao.impl.BaseDao;
import com.zd.commodity.userMng.entity.User;

public class BrandShowDaoImpl extends BaseDao<BrandShow> implements IBrandShowDao {

	@Override
	public DetachedCriteria getDetachedCriteria(BrandShow t1,BrandShow t2,Object param){
		DetachedCriteria dc=DetachedCriteria.forClass(BrandShow.class);
		if(t1!=null){
			if(!"".equals(t1.getName())){
				dc.add(Restrictions.like("name", t1.getName(), MatchMode.ANYWHERE));
			}
		}
		dc.addOrder(Order.desc("creationtime"));
		return dc;
	}
	
	@Override
	public void add(BrandShow t) {
		User user=(User)ServletActionContext.getRequest().getSession().getAttribute("user");
		Timestamp time=new Timestamp(System.currentTimeMillis());
		
		t.setName(t.getName());
		t.setDescription(t.getDescription());
		t.setCreator(user.getUsername());
		t.setCreationtime(time);
		
		getHibernateTemplate().save(t);		
	}
	
	@Override
	public void update(BrandShow t) {

		User user=(User)ServletActionContext.getRequest().getSession().getAttribute("user");
		Timestamp time=new Timestamp(System.currentTimeMillis());
		
		t.setName(t.getName());
		t.setDescription(t.getDescription());
		t.setCreator(t.getCreator());
		t.setCreationtime(t.getCreationtime());
		t.setUpdatedtime(time);
		t.setUpdater(user.getUsername());
		
		getHibernateTemplate().update(t);
	}
}
